package edu.jptest.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.util.Date;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import edu.jptest.bean.Group;
import edu.jptest.bean.User;
import edu.jptest.serviceImp.GroupService;
import edu.jptest.serviceImp.UserService;
import edu.jptest.util.MD5Encoding;

public class UserAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = -198171554828407704L;
	// 输入密码时，需要两次输入
	private String repwd;
	// 更新后的User
	private User user;
	// 封装上传文件域的属性
	private File userHead;
	// 封装上传文件类型的属性
	private String userHeadContentType;
	// 封装上传文件名
	private String userHeadFileName;
	// 文件保存路径，接受依赖注入的属性,在struts.xml文件里声明
	private String savePath;
	private Group group;

	public Group getGroup() {
		return group;
	}

	public void setGroup(Group group) {
		this.group = group;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	// 得到上传文件的实际保存位置
	public String getSavePath() throws Exception {
		return ServletActionContext.getServletContext().getRealPath(savePath);
	}

	public File getUserHead() {
		return userHead;
	}

	public void setUserHead(File userHead) {
		this.userHead = userHead;
	}

	public String getRepwd() {
		return repwd;
	}

	public void setRepwd(String repwd) {
		this.repwd = repwd;
	}

	public String getUserHeadContentType() {
		return userHeadContentType;
	}

	public String getUserHeadFileName() {
		return userHeadFileName;
	}

	public void setUserHeadContentType(String userHeadContentType) {
		this.userHeadContentType = userHeadContentType;
	}

	public void setUserHeadFileName(String userHeadFileName) {
		this.userHeadFileName = userHeadFileName;
	}

	// 进入修改个人信息页面
	@SuppressWarnings("unchecked")
	public String ModifyUserInfo() {
		Map session = ActionContext.getContext().getSession();
		user = (User) session.get("user");
		System.out.println("user name: " + user.getUserNickname()
				+ user.getUserName() + user.getUserSex() + "user group id: "
				+ user.getGroup().getGroupId());
		// user = userService.getUserByUserId(user.getUserId());
		return SUCCESS;
	}

	// 修改个人信息
	@SuppressWarnings("unchecked")
	public String modifyUser() {
		UserService userService = new UserService();
		User userDemo = new User();
		Map session = ActionContext.getContext().getSession();
		user = (User) session.get("user");
		userDemo = userService.getUserByUserId(user.getUserId());
		System.out.println("user groupId: " + userDemo.getGroup().getGroupId());
		userDemo
				.setUserIco(Integer.toString((int) (Math.random() * 10000))
						+ DateFormat.getDateInstance().format(new Date())
						+ "."
						+ getUserHeadContentType().split("[/]")[getUserHeadContentType()
								.split("[/]").length - 1]);
		FileInputStream in;
		try {
			FileOutputStream out = new FileOutputStream(this.getSavePath()
					+ "//" + userDemo.getUserIco());// 在服务器上设置上传文件输出流
			in = new FileInputStream(this.getUserHead());
			byte[] buffer = new byte[1024];
			int len;
			try {
				while ((len = in.read(buffer)) > 0) {
					try {
						out.write(buffer, 0, len);
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			} catch (IOException e) {
				e.printStackTrace();
			}// 上传音频写入文件
		} catch (Exception e) {
			e.printStackTrace();
		}
		userDemo.setUserEmail(user.getUserEmail());
		userDemo.setUserPassword(MD5Encoding.getMD5ofStr(repwd));
		userDemo.setUserName(user.getUserName());
		GroupService groupService = new GroupService();
		System.out.println("group name,class name: " + group.getGroupName()
				+ group.getClassName());
		group = groupService.getGroupByName(group.getGroupName(), group
				.getClassName());
		userDemo.setGroup(group);
		if (user.getUserSex().equals("0"))
			userDemo.setUserSex("男");
		else if (user.getUserSex().equals("1")) {
			userDemo.setUserSex("女");
		}
		userService.modifyUser(userDemo);
		return SUCCESS;
	}

	// 查看个人信息
	public String viewUserInfo() {
		UserService userService = new UserService();
		user = userService.getUserByUserId(user.getUserId());
		return SUCCESS;
	}
}
